<template>
  <div class="top-right-btn" :style="style">
    <el-row>
      <el-tooltip
        class="item"
        effect="dark"
        :content="showSearch ? '隐藏搜索' : '显示搜索'"
        placement="top"
        v-if="search"
      >
        <el-button size="small" circle icon="Search" @click="toggleSearch()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="刷新" placement="top">
        <el-button size="small" circle icon="Refresh" @click="refresh()" />
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="显隐列" placement="top" v-if="columns">
        <el-button
          size="small"
          circle
          icon="Menu"
          @click="showColumn()"
          v-if="showColumnsType == 'transfer'"
        />
        <el-dropdown
          trigger="click"
          :hide-on-click="false"
          style="padding-left: 12px"
          v-if="showColumnsType == 'checkbox'"
        >
          <el-button size="small" circle icon="Menu" />
          <template #dropdown>
            <el-dropdown-menu>
              <template v-for="item in columns" :key="item.key">
                <el-dropdown-item>
                  <el-checkbox
                    :checked="item.visible"
                    @change="(val: boolean | number | string) => checkboxChange(val, item.label)"
                    :label="item.label"
                  />
                </el-dropdown-item>
              </template>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </el-tooltip>
    </el-row>
    <el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false">
      <el-transfer
        :titles="['显示', '隐藏']"
        v-model="value"
        :data="columns"
        @change="dataChange"
      ></el-transfer>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { computed, ref, watch } from 'vue'

interface Column {
  key: number
  label: string
  visible: boolean
}

interface Props {
  showSearch?: boolean
  columns?: Column[]
  search?: boolean
  showColumnsType?: string
  gutter?: number
}

const props = withDefaults(defineProps<Props>(), {
  showSearch: true,
  columns: () => [],
  search: true,
  showColumnsType: 'checkbox',
  gutter: 10,
})

const emit = defineEmits<{
  (e: 'update:showSearch', value: boolean): void
  (e: 'queryTable'): void
}>()

// 显隐数据
const value = ref<number[]>([])
// 弹出层标题
const title = ref('显示/隐藏')
// 是否显示弹出层
const open = ref(false)

// 计算样式
const style = computed(() => {
  const ret: { marginRight?: string } = {}
  if (props.gutter) {
    ret.marginRight = `${props.gutter / 2}px`
  }
  return ret
})

// 初始化显隐列
watch(
  () => props.columns,
  (newColumns) => {
    if (props.showColumnsType == 'transfer' && newColumns) {
      // 显隐列初始默认隐藏列
      value.value = []
      newColumns.forEach((item, index) => {
        if (item.visible === false) {
          value.value.push(index)
        }
      })
    }
  },
  { immediate: true },
)

// 搜索
const toggleSearch = () => {
  emit('update:showSearch', !props.showSearch)
}

// 刷新
const refresh = () => {
  emit('queryTable')
}

// 右侧列表元素变化
const dataChange: Fn = (data: number[]) => {
  if (props.columns) {
    props.columns.forEach((item, index) => {
      item.visible = !data.includes(index)
    })
  }
}

// 打开显隐列dialog
const showColumn = () => {
  open.value = true
}

// 勾选
const checkboxChange = (event: boolean | number | string, label: string) => {
  if (props.columns) {
    const targetColumn = props.columns.find((item) => item.label === label)
    if (targetColumn) {
      targetColumn.visible = Boolean(event)
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-transfer__button) {
  border-radius: 50%;
  padding: 12px;
  display: block;
  margin-left: 0px;

  &:first-child {
    margin-bottom: 10px;
  }
}
</style>
